import smilea from './assets/Images/Expression/smilea_thumb.gif';
import tootha from './assets/Images/Expression/tootha_thumb.gif';
import laugh from './assets/Images/Expression/laugh.gif';

interface myRefType {
  current: HTMLDivElement | null | string | undefined;
}
// 当内容变多的的时候，滚动到到底部
export function toBottom(time: number, ref: myRefType) {
  const timerId: ReturnType<typeof setTimeout> = setTimeout(() => {
    if (ref.current) {
      const chatContent = ref.current as HTMLDivElement;
      const scrollHeight = chatContent.scrollHeight;
      chatContent.scrollTop = scrollHeight + 200;
    }
    clearTimeout(timerId);
  }, time);
}

// 表情列表
export const expressions = [
  {
    title: '[呵呵]',
    image: smilea,
  },
  {
    title: '[嘻嘻]',
    image: tootha,
  },
  {
    title: '[哈哈]',
    image: laugh,
  },
];

// interface HTMLInputEvent extends Event {
//   target: HTMLInputElement & EventTarget;
// }
//发送图片
export function sendImage(
  e: React.ChangeEvent<HTMLInputElement>,
  identity: number,
  callback: any
) {
  let fileObj = null;
  if (e.target && e.target.files) {
    fileObj = e.target.files[0];
  }

  // let identity = this.sender.isService ? 1 : 2;
  if (fileObj != null) {
    // 判断是否是图片
    if (!/image\/\w+/.test(fileObj.type)) {
      return alert('请选择图片文件!');
    }
    // const fd = new FormData();
    // fd.append('file', fileObj);

    // 判断图片大小,大于2M小于10M
    if (fileObj.size > 1024 * 1024 * 2) {
      return alert('上传图片不能超过2M!');
    } else {
      const reader = new FileReader();
      // 读取成字符串
      reader.readAsDataURL(fileObj);
      reader.addEventListener(
        'load',
        (e) => {
          // convert image file to base64 string
          if (e.target) {
            callback(e.target.result, identity, 1);
          }
        },
        false
      );
    }
  }
}
